import 'package:flutter/material.dart';
import 'package:thingsboard_app/core/context/tb_context.dart';
import 'package:thingsboard_app/core/context/tb_context_widget.dart';
import 'package:thingsboard_app/core/entity/entities_base.dart';
import 'package:thingsboard_app/widgets/tb_app_bar.dart';

import 'assets_list.dart';

class AssetsPage extends TbPageWidget {

  final bool searchMode;

  AssetsPage(TbContext tbContext, {this.searchMode = false}) : super(tbContext);

  @override
  _AssetsPageState createState() => _AssetsPageState();

}

class _AssetsPageState extends TbPageState<AssetsPage> {

  final PageLinkController _pageLinkController = PageLinkController();

  @override
  Widget build(BuildContext context) {
    var assetsList = AssetsList(tbContext, _pageLinkController, searchMode: widget.searchMode);
    PreferredSizeWidget appBar;
    if (widget.searchMode) {
      appBar = TbAppSearchBar(
        tbContext,
        onSearch: (searchText) => _pageLinkController.onSearchText(searchText),
      );
    } else {
      appBar = TbAppBar(
          tbContext,
          title: Text(assetsList.title),
          actions: [
            IconButton(
              icon: Icon(
                  Icons.search
              ),
              onPressed: () {
                navigateTo('/assets?search=true');
              },
            )
          ]);
    }
    return Scaffold(
        appBar: appBar,
        body: assetsList
    );
  }

  @override
  void dispose() {
    _pageLinkController.dispose();
    super.dispose();
  }

}
